//
//  AppearanceCommunity.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/9.
//  Copyright © 2016年 KX. All rights reserved.
#import "ModelOfAppearanceCommunity.h"
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "AppearanceCommunity.h"
#import "CellOfAppearanceCommunity.h"
#import "CellOfAppearanceCommunityTitle.h"
#import "ModelOfAppearanceCommunityForum_list.h"
#import "UIImageView+WebCache.h"
#import "ModelOfAppearanceCommunityGroup.h"
#import "BBSViewController.h"
#import "CommunityViewController.h"
@interface AppearanceCommunity ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *titleTableView;
@property (nonatomic, strong) ModelOfAppearanceCommunity *model;
@property (nonatomic, assign) NSInteger currentInteger;
@property (nonatomic, strong) NSIndexPath *oldIndexPath;
@end
@implementation AppearanceCommunity
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.oldIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self handleData];
        [self createTableView];
        
        self.currentInteger = 0;
    }
    return self;
}
- (void)handleData{
    [FreeTravelNetwork GET:@"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&page=1&track_app_channel=App%2520Store&track_app_version=7.0.1&track_device_info=iPhone%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%25209.3.4&v=1" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [ModelOfAppearanceCommunity modelWithDic:result];
        
        [self.tableView reloadData];
        [self.titleTableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   
    [self.tableView registerNib:[UINib nibWithNibName:@"CellOfAppearanceCommunity" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CellOfAppearanceCommunity"];
    
    self.titleTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.titleTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.titleTableView];
    self.titleTableView.delegate = self;
    self.titleTableView.dataSource = self;
    [self.titleTableView registerClass:[CellOfAppearanceCommunityTitle class] forCellReuseIdentifier:@"CellOfAppearanceCommunityTitle"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.titleTableView) {
        
        return self.model.data.forum_list.count;
    }else{
        ModelOfAppearanceCommunityForum_list *model = self.model.data.forum_list[self.currentInteger];
        return model.group.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.titleTableView) {
        CellOfAppearanceCommunityTitle *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfAppearanceCommunityTitle" forIndexPath:indexPath];
        ModelOfAppearanceCommunityForum_list *model = self.model.data.forum_list[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.label.text = model.name;
        if (indexPath.row == self.oldIndexPath.row) {
            cell.label.textColor = COLORFORFREETRAVEL;
            cell.label.backgroundColor = [UIColor whiteColor];
        }else{
            cell.label.textColor = [UIColor blackColor];
            cell.label.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00];
        }
        return cell;
    }else{
        CellOfAppearanceCommunity *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfAppearanceCommunity" forIndexPath:indexPath];
        ModelOfAppearanceCommunityForum_list *model = self.model.data.forum_list[self.currentInteger];
        ModelOfAppearanceCommunityGroup *groupModel = model.group[indexPath.row];
        [cell.imageViewOfPhoto sd_setImageWithURL:[NSURL URLWithString:groupModel.photo]];
        cell.labelOfName.text = groupModel.name;
        cell.labelOfDescription.text = groupModel.Description;
        cell.labelOfTotal_threads.text = groupModel.total_threads;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.titleTableView) {
        
        self.currentInteger = indexPath.row;
        CellOfAppearanceCommunityTitle *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.label.backgroundColor = [UIColor whiteColor];
        cell.label.textColor = COLORFORFREETRAVEL;
        CellOfAppearanceCommunityTitle *oldCell = [tableView cellForRowAtIndexPath:_oldIndexPath];
        oldCell.label.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.00];
        oldCell.label.textColor = [UIColor blackColor];
        self.oldIndexPath = indexPath;
        [self.tableView reloadData];
    }else{
        BBSViewController *vc = [[BBSViewController alloc]init];
        CommunityViewController *communitu = (CommunityViewController *)self.nextResponder.nextResponder.nextResponder;
        
        ModelOfAppearanceCommunityForum_list *model = self.model.data.forum_list[self.currentInteger];
        ModelOfAppearanceCommunityGroup *groupModel = model.group[indexPath.row];
        vc.ID = groupModel.ID;
        [communitu.navigationController pushViewController:vc animated:YES];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.titleTableView) {
        return 80;
    }else{
        return 100;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(SCREEN_WIDTH/3, 0, SCREEN_WIDTH/3*2, SCREEN_HEIGHT-108);
    self.titleTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, SCREEN_HEIGHT-108);
}
@end
